package org.weineng.login.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.weineng.login.service.*;
import org.weineng.login.vo.User;
public class LoginAction extends ActionSupport{
	private User user;
	private String username;
	private String password;
	private String kaptcha;
	protected IUserService userService;

	public String register(){
		User u=new User(user.getUsername(),user.getPassword());
		if(userService.registerUser(u)!=null)
		{
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String execute() throws Exception{
		String p;
		String kaptchaExpected = (String)ActionContext.getContext().getSession().get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		//System.out.println(kaptchaExpected);
		//System.out.println(getKaptcha());
		p=getKaptcha().toLowerCase();
		if(p==null||!p.equals(kaptchaExpected))
		return INPUT;
		
		boolean validated=false;
		User u=userService.validateUser(user.getUsername(), user.getPassword());
		if(u!=null)
		{
			validated=true;
		}
		if(validated)
		{
			return SUCCESS;
		}
		else
		{
			return ERROR;
		}
	}
	
	public User getUser(){
		return user;
	}
	public void setUser(User user){
		this.user=user;
	}
	public IUserService getUserService(){
		return this.userService;
	}
	public void setUserService(IUserService userService){
		this.userService=userService;
	}
	public String getKaptcha() {
		return this.kaptcha;
	}
	
	public void setKaptcha(String kaptcha) {
		this.kaptcha = kaptcha;
	}

}